package com.lzw.controller.dormitoryAdmin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.Announcement;
import com.lzw.service.dormitoryAdmin.AnnounceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/announcement2")
@CrossOrigin(origins = "http://localhost:7070")
public class AnnounceController {

    @Autowired
    private AnnounceService announcementService;

    //分页查询
    @GetMapping("/list")
    public PageInfo<Announcement> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate datePosted,
            String postedBy) {
        PageHelper.startPage(pageNum, pageSize);
        List<Announcement> list = announcementService.list(pageNum, pageSize, datePosted, postedBy);
        return new PageInfo<>(list);
    }

    //发布
    @Log
    @PostMapping("/add")
    public String update(@RequestBody Announcement announcement){
        announcementService.add(announcement);
        return "发布成功";
    }
}